﻿// owner 蒋家伟

#include "BitoperationUnitTest.h"

// std
#include <cmath>

// jsdk
#include "UnitTestDataDefine.h"
#include "UnitTestDbgWarnDefine.h"
#include "OwnerPtrUtils.h"
#include "BitoperationUtils.h"
#include "UnitTestUtils.h"

using namespace jsdk;

UNIT_TEST_START(jsdk, BitoperationUnitTest)
{
    BitoperationUnitTest test;
    bool success = test.testPower();
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(success, L"2指数位操作功能测试失败", L"蒋家伟", L"2023-11-10");
    success = test.testSectionValue();
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(success, L"位操作去部分值功能测试失败", L"蒋家伟", L"2023-11-10");
    return true;
}

bool jsdk::BitoperationUnitTest::testPower() const
{
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(std::pow(2, 1) == jsdk::Power_Two<1>::value, UnitTestUtils::combineErrorTestMsg(jsdk::Power_Two<1>::value, std::pow(2, 1), L"2的1次方"), L"蒋家伟", L"2023-11-27");
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(std::pow(2, 2) == jsdk::Power_Two<2>::value, UnitTestUtils::combineErrorTestMsg(jsdk::Power_Two<2>::value, std::pow(2, 2), L"2的2次方"), L"蒋家伟", L"2023-11-27");
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(std::pow(2, 4) == jsdk::Power_Two<4>::value, UnitTestUtils::combineErrorTestMsg(jsdk::Power_Two<4>::value, std::pow(2, 4), L"2的4次方"), L"蒋家伟", L"2023-11-27");
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(std::pow(2, 8) == jsdk::Power_Two<8>::value, UnitTestUtils::combineErrorTestMsg(jsdk::Power_Two<8>::value, std::pow(2, 8), L"2的8次方"), L"蒋家伟", L"2023-11-27");
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(std::pow(2, 16) == jsdk::Power_Two<16>::value, UnitTestUtils::combineErrorTestMsg(jsdk::Power_Two<16>::value, std::pow(2, 16), L"2的16次方"), L"蒋家伟", L"2023-11-27");
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(std::pow(2, 32) == jsdk::Power_Two<32>::value, UnitTestUtils::combineErrorTestMsg(jsdk::Power_Two<32>::value, std::pow(2, 32), L"2的32次方"), L"蒋家伟", L"2023-11-27");
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(std::pow(2, 63) == jsdk::Power_Two<63>::value, UnitTestUtils::combineErrorTestMsg(jsdk::Power_Two<63>::value, std::pow(2, 63), L"2的63次方"), L"蒋家伟", L"2023-11-27");

    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(Is_Power_Two<jsdk::Power_Two<1>::value>::value, UnitTestUtils::combineErrorTestMsg(false, true, L"2的1次方"), L"蒋家伟", L"2023-11-27");
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(Is_Power_Two<jsdk::Power_Two<2>::value>::value, UnitTestUtils::combineErrorTestMsg(false, true, L"2的2次方"), L"蒋家伟", L"2023-11-27");
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(Is_Power_Two<jsdk::Power_Two<3>::value>::value, UnitTestUtils::combineErrorTestMsg(false, true, L"2的3次方"), L"蒋家伟", L"2023-11-27");
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(Is_Power_Two<jsdk::Power_Two<5>::value>::value, UnitTestUtils::combineErrorTestMsg(false, true, L"2的5次方"), L"蒋家伟", L"2023-11-27");
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(Is_Power_Two<jsdk::Power_Two<8>::value>::value, UnitTestUtils::combineErrorTestMsg(false, true, L"2的8次方"), L"蒋家伟", L"2023-11-27");
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(Is_Power_Two<jsdk::Power_Two<13>::value>::value, UnitTestUtils::combineErrorTestMsg(false, true, L"2的13次方"), L"蒋家伟", L"2023-11-27");
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(!Is_Power_Two<3>::value, UnitTestUtils::combineErrorTestMsg(true, false, L"3"), L"蒋家伟", L"2023-11-27");
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(!Is_Power_Two<-1>::value, UnitTestUtils::combineErrorTestMsg(true, false, L"-1"), L"蒋家伟", L"2023-11-27");

    return true;
}

bool jsdk::BitoperationUnitTest::testSectionValue() const
{
    {
        int32_t value = 0xFF'AD'99'88;
        // 1、有符号
        int32_t value0_7 = jsdk::getIndexSectionValue<0, 7>(value);
        UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(value0_7 == 0x88, UnitTestUtils::combineErrorTestMsg(value0_7, 0x88, StringUtils::toWString(value) + L"0-7位"), L"蒋家伟", L"2023-11-27");
        int32_t value8_15 = jsdk::getIndexSectionValue<8, 15>(value);
        UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(value8_15 == 0x99, UnitTestUtils::combineErrorTestMsg(value8_15, 0x99, StringUtils::toWString(value) + L"8-15位"), L"蒋家伟", L"2023-11-27");
        int32_t value16_23 = jsdk::getIndexSectionValue<16, 23>(value);
        UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(value16_23 == 0xAD, UnitTestUtils::combineErrorTestMsg(value16_23, 0xAD, StringUtils::toWString(value) + L"16-23位"), L"蒋家伟", L"2023-11-27");
        int32_t value24_31 = jsdk::getIndexSectionValue<24, 31>(value);
        UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(value24_31 == 0xFF, UnitTestUtils::combineErrorTestMsg(value24_31, 0xFF, StringUtils::toWString(value) + L"24-31位"), L"蒋家伟", L"2023-11-27");

        // 特殊位
        int32_t value0_31 = jsdk::getIndexSectionValue<0, 31>(value);
        UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(value0_31 == value, UnitTestUtils::combineErrorTestMsg(value0_31, value, StringUtils::toWString(value) + L"0-31位"), L"蒋家伟", L"2023-11-27");
        int32_t value3_3 = jsdk::getIndexSectionValue<3, 3>(value);
        UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(value3_3 == 1, UnitTestUtils::combineErrorTestMsg(value3_3, 1, StringUtils::toWString(value) + L"3-3位"), L"蒋家伟", L"2023-11-27");
        int32_t value0_3 = jsdk::getIndexSectionValue<0, 3>(value);
        UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(value0_3 == 8, UnitTestUtils::combineErrorTestMsg(value0_3, 8, StringUtils::toWString(value) + L"0-3位"), L"蒋家伟", L"2023-11-27");
        int32_t value1_3 = jsdk::getIndexSectionValue<1, 3>(value);
        UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(value1_3 == 4, UnitTestUtils::combineErrorTestMsg(value1_3, 4, StringUtils::toWString(value) + L"1-3位"), L"蒋家伟", L"2023-11-27");
        int32_t value10_9 = jsdk::getIndexSectionValue<10, 9>(value);
        UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(value10_9 == 0, UnitTestUtils::combineErrorTestMsg(value10_9, 0, StringUtils::toWString(value) + L"10-9位"), L"蒋家伟", L"2023-11-27");
    }

    {
        uint32_t value = 0xFF'AD'99'88;
        // 2、无符号
        uint32_t value0_7 = jsdk::getIndexSectionValue<0, 7>(value);
        UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(value0_7 == 0x88, UnitTestUtils::combineErrorTestMsg(value0_7, 0x88, StringUtils::toWString(value) + L"0-7位"), L"蒋家伟", L"2023-11-27");
        uint32_t value8_15 = jsdk::getIndexSectionValue<8, 15>(value);
        UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(value8_15 == 0x99, UnitTestUtils::combineErrorTestMsg(value8_15, 0x99, StringUtils::toWString(value) + L"8-15位"), L"蒋家伟", L"2023-11-27");
        uint32_t value16_23 = jsdk::getIndexSectionValue<16, 23>(value);
        UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(value16_23 == 0xAD, UnitTestUtils::combineErrorTestMsg(value16_23, 0xAD, StringUtils::toWString(value) + L"16-23位"), L"蒋家伟", L"2023-11-27");
        uint32_t value24_31 = jsdk::getIndexSectionValue<24, 31>(value);
        UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(value24_31 == 0xFF, UnitTestUtils::combineErrorTestMsg(value24_31, 0xFF, StringUtils::toWString(value) + L"24-31位"), L"蒋家伟", L"2023-11-27");

        // 特殊位
        uint32_t value0_31 = jsdk::getIndexSectionValue<0, 31>(value);
        UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(value0_31 == value, UnitTestUtils::combineErrorTestMsg(value0_31, value, StringUtils::toWString(value) + L"0-31位"), L"蒋家伟", L"2023-11-27");
        uint32_t value3_3 = jsdk::getIndexSectionValue<3, 3>(value);
        UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(value3_3 == 1, UnitTestUtils::combineErrorTestMsg(value3_3, 1, StringUtils::toWString(value) + L"3-3位"), L"蒋家伟", L"2023-11-27");
        uint32_t value0_3 = jsdk::getIndexSectionValue<0, 3>(value);
        UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(value0_3 == 8, UnitTestUtils::combineErrorTestMsg(value0_3, 8, StringUtils::toWString(value) + L"0-3位"), L"蒋家伟", L"2023-11-27");
        uint32_t value1_3 = jsdk::getIndexSectionValue<1, 3>(value);
        UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(value1_3 == 4, UnitTestUtils::combineErrorTestMsg(value1_3, 4, StringUtils::toWString(value) + L"1-3位"), L"蒋家伟", L"2023-11-27");
        uint32_t value10_9 = jsdk::getIndexSectionValue<10, 9>(value);
        UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(value10_9 == 0, UnitTestUtils::combineErrorTestMsg(value10_9, 0, StringUtils::toWString(value) + L"10-9位"), L"蒋家伟", L"2023-11-27");
    }

    return true;
}
